package com.dj.mingkong.admin.biz.service.music.impl;

import com.dj.mingkong.admin.biz.controller.music.vo.request.AlbumMusicUpdateVo;
import com.dj.mingkong.admin.biz.controller.music.vo.response.AlbumMusicVo;
import com.dj.mingkong.admin.biz.repository.database.music.AlbumMusicRepository;
import com.dj.mingkong.admin.biz.repository.database.music.AlbumRepository;
import com.dj.mingkong.admin.biz.repository.database.music.MusicRepository;
import com.dj.mingkong.admin.biz.service.music.AlbumMusicService;
import com.dj.mingkong.common.infrastructure.base.eo.BaseEo;
import com.dj.mingkong.common.infrastructure.eo.database.music.AlbumMusicEo;
import com.dj.mingkong.common.util.AssertUtils;
import com.google.common.collect.Lists;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 音乐专辑服务实现
 *
 * @author zenglonghui
 * @date: 2022/8/16 15:35
 */
@Slf4j
@Service("albumMusicService")
public class AlbumMusicServiceImpl implements AlbumMusicService {
    @Resource
    private AlbumMusicRepository albumMusicRepository;
    @Resource
    private MusicRepository musicRepository;
    @Resource
    private AlbumRepository albumRepository;

    @Override
    public AlbumMusicVo listByMusicId(Long musicId) {
        final List<AlbumMusicEo> albumMusicEos = albumMusicRepository.listByMusicId(musicId);
        if (albumMusicEos.isEmpty()) {
            return AlbumMusicVo.builder().musicId(musicId).albumIdList(Lists.newArrayList())
                    .build();
        }
        return AlbumMusicVo.builder().musicId(musicId)
                .albumIdList(albumMusicEos.stream().map(AlbumMusicEo::getAlbumId).toList())
                .build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByMusicId(AlbumMusicUpdateVo updateVo) {
        AssertUtils.isNotNull(musicRepository.getById(updateVo.getMusicId()), "musicId invalid");
        final List<Long> albumIdList = updateVo.getAlbumIdList();
        final List<AlbumMusicEo> albumMusicEos = albumMusicRepository.listByMusicId(updateVo.getMusicId());
        if (CollectionUtils.isNotEmpty(albumMusicEos)) {
            final List<Long> addList = albumIdList.stream().filter(aLong ->
                    !albumMusicEos.stream().map(AlbumMusicEo::getAlbumId).toList().contains(aLong)
                            && Objects.nonNull(albumRepository.getById(aLong))).toList();
            saveAlbumMusic(addList, updateVo.getMusicId());
            final List<Long> delList = albumMusicEos.stream().filter(albumMusicEo ->
                    !albumIdList.contains(albumMusicEo.getAlbumId())).map(BaseEo::getId).toList();
            albumMusicRepository.deleteByIds(delList);
        } else {
            saveAlbumMusic(albumIdList, updateVo.getMusicId());
        }
    }

    private void saveAlbumMusic(List<Long> albumIdList, Long musicId) {
        if (CollectionUtils.isNotEmpty(albumIdList)) {
            final List<AlbumMusicEo> albumMusicEos = albumIdList.stream().map(aLong ->
                    AlbumMusicEo.builder().albumId(aLong).musicId(musicId).build()).toList();
            albumMusicRepository.saveBatch(albumMusicEos);
        }
    }
}
